package ik.xcj.hostpital.manage.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import ik.xcj.hostpital.manage.pojo.dto.OrderInfoAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.OrderInfoUpdateDTO;
import ik.xcj.hostpital.manage.pojo.vo.OrderCountQueryVo;
import ik.xcj.hostpital.manage.pojo.vo.OrderListItemInfoVO;
import ik.xcj.hostpital.manage.pojo.vo.OrderStandardInfoVO;
import ik.xcj.hostpital.manage.service.IOrderInfoService;
import ik.xcj.hostpital.manage.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 处理订单相关请求的控制器
 *
 * @author java@ik.xcj
 * @version 0.0.1
 */

@Slf4j
@RestController
@Api(tags = "订单管理模块")
@RequestMapping("/orderInfo")
public class OrderInfoController {

    @Autowired
    private IOrderInfoService orderInfoService;

    public OrderInfoController() {
        log.info("创建控制器：OrderInfoController");
    }

    // http://localhost:9080/orderInfo/add-new
    @ApiOperation("添加订单详情")
    @ApiOperationSupport(order = 110)
    @PostMapping("/add-new")
    public JsonResult<Void> insert(OrderInfoAddNewDTO orderInfoAddNewDTO){
        orderInfoService.addNew(orderInfoAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询订单详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id",value = "订单id",required = true,dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<OrderStandardInfoVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据id查询订单详情】的请求，参数：{}", id);
        OrderStandardInfoVO order = orderInfoService.getStandardById(id);
        return JsonResult.ok(order);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除订单")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@Range(min = 1, max = 1000000, message = "删除订单失败，订单ID非法！")
                             @PathVariable Long id){
        log.debug("开始处理【根据id删除订单】的请求，参数：{}",id);
        orderInfoService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation("分页查询订单列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example ="1" ),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example ="10" )
    })
    @PostMapping ("/page")
    public JsonResult<PageInfo<OrderListItemInfoVO>> list(Integer page,Integer pageSize) {
        log.debug("开始处理【查询订单模板列表】的请求，无参数");
        PageInfo<OrderListItemInfoVO> list = orderInfoService.list(page, pageSize);
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/getCountMap")
    @PreAuthorize("hasAuthority('/sys/order/read')")
    @ApiOperationSupport(order = 420)
    public Map<String,Object>  getCountMap(OrderCountQueryVo orderCountQueryVo){
        return orderInfoService.getCountMap(orderCountQueryVo);
    }
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改订单数据")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/sys/order/update')")
    public JsonResult updateInfoById(@PathVariable Long id, OrderInfoUpdateDTO orderInfoUpdateDTO) {
        log.debug("开始处理【修改订单详情】的请求，参数：{}", orderInfoUpdateDTO);
        orderInfoService.updateInfoById(id, orderInfoUpdateDTO);
        return JsonResult.ok();
    }

}
